﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Cleverscape.UTorrentClient.WebClient;
using GalaSoft.MvvmLight.Messaging;

namespace uTorrentSL.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class FeedViewModel : ViewModelBase
    {

        public RelayCommand SaveFeed { get; set; }

        public RelayCommand RemoveFeed { get; set; }
        /// <summary>
        /// The <see cref="SelectedFilter" /> property's name.
        /// </summary>
        public const string SelectedFeedPropertyName = "SelectedFeed";

        private Feed _selectedFeed = null;

        /// <summary>
        /// Gets the SelectedFilter property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public Feed SelectedFeed
        {
            get
            {
                return _selectedFeed;
            }

            set
            {
                if (_selectedFeed == value)
                {
                    return;
                }

                var oldValue = _selectedFeed;
                _selectedFeed = value;

                // Remove one of the two calls below
                //throw new NotImplementedException();

                // Update bindings, no broadcast
                RaisePropertyChanged(SelectedFeedPropertyName);

                // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
                //RaisePropertyChanged(SelectedFilterPropertyName, oldValue, value, true);
            }
        }

        /// <summary>
        /// Initializes a new instance of the FilterViewModel class.
        /// </summary>
        public FeedViewModel()
        {
            if (IsInDesignMode)
            {
                SelectedFeed = new Feed();
            }
            ////else
            ////{
            ////    // Code runs "for real": Connect to service, etc...
            ////}
            Messenger.Default.Register<Feed>(this, "SetFeed", SetSelectedFeed);

            SaveFeed = new RelayCommand(() =>
            {
                ViewModelLocator.DataService.FeedUpdateAdd(this.SelectedFeed);
                ViewModelLocator.GoBack();
            });

            RemoveFeed = new RelayCommand(() =>
            {
                ViewModelLocator.DataService.FeedRemove(this.SelectedFeed);
                ViewModelLocator.GoBack();
            });
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean own resources if needed

        ////    base.Cleanup();
        ////}

        public void SetSelectedFeed(Feed sf)
        {
            this.SelectedFeed = sf;
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean own resources if needed

        ////    base.Cleanup();
        ////}
    }
}